package com.shbaiche.bifenba.adapter.video;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.kit.AppKit;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.video.ArticleCollectMessage;
import com.shbaiche.bifenba.model.video.ArticleDetailMessage;
import com.shbaiche.bifenba.model.video.ArticleDiscussVo;
import com.shbaiche.bifenba.model.video.DiscussFollowMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.my.LoginActivity;
import com.shbaiche.bifenba.ui.video.VideoDetailActivity;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;
import com.shbaiche.bifenba.view.CircularImage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class VideoDetailAdapter extends BaseAdapter {
    private Context context;

    private List<ArticleDiscussVo> his;

    private Map<Integer, Boolean> clickMap = new HashMap<>();

    public VideoDetailAdapter(Context context, List<ArticleDiscussVo> his) {
        super();
        this.context = context;
        this.his = his;
    }

    public void setData(List<ArticleDiscussVo> his) {
        this.his = his;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (his == null)
            return 0;
        return his.size();
    }

    public void addNewData(List<ArticleDiscussVo> data) {
        if (data != null && data.size() > 0) {
            his.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public ArticleDiscussVo getItem(int arg0) {
        return his.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View v, ViewGroup arg2) {
        ViewHolder holder = null;
        if (v == null) {
            holder = new ViewHolder();
            v = LayoutInflater.from(context).inflate(R.layout.adapter_video_detail_item, null);
            holder.name = (TextView) v.findViewById(R.id.tv_video_name);
            holder.content = (TextView) v.findViewById(R.id.tv_video_content);
            holder.time = (TextView) v.findViewById(R.id.tv_video_time);
            holder.dianzan = (TextView) v.findViewById(R.id.tv_video_comment);
            holder.head = (CircularImage) v.findViewById(R.id.img_video_icon);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();

        }
        final ArticleDiscussVo item = getItem(position);

        Glide.with(context).load(item.getPhoto()).placeholder(R.drawable.ic_my_head).error(R.drawable.ic_my_head).into(holder.head);
        String name = item.getUserName();
        if (AppKit.isMobileNum(name)) {
            name = name.substring(0, 3) + "****" + name.substring(7, name.length());
        }
        holder.name.setText(name);
        holder.content.setText(item.getContent() + "");
        holder.time.setText(item.getPublishTime() + "");
        long num = 0;
        if (item.getFollowNum() != null) {
            num = item.getFollowNum();
        }
        holder.dianzan.setText(num + "");

        if (clickMap.get(position) != null && clickMap.get(position)) {
            holder.dianzan.setCompoundDrawablesWithIntrinsicBounds(null,
                    null, context.getResources().getDrawable(R.mipmap.zan_press), null);
            holder.dianzan.setTextColor(Color.parseColor("#2479E2"));
        } else {
            holder.dianzan.setCompoundDrawablesWithIntrinsicBounds(null,
                    null, context.getResources().getDrawable(R.mipmap.zan_normall), null);
            holder.dianzan.setTextColor(Color.parseColor("#333333"));
            holder.dianzan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (App.getPassport() == null) {
                        ToastUtil.show("请先登录!");
                        LoginActivity.launch((Activity) context);
                        return;
                    }

                    if (clickMap.get(position) != null && clickMap.get(position)) {
                        return;
                    }
                    clickMap.put(position, true);

                    notifyDataSetChanged();

                    DiscussFollowMessage msg = new DiscussFollowMessage();
                    msg.setPassport(App.getPassport());
                    msg.setDiscussId(item.getId());

                    NetApi.ArticleDiscussFollow(msg, new JsonCallback<TransMessage>() {

                        @Override
                        public void onFail(Call call, Exception e, int id) {
                        }

                        @Override
                        public void onError(Call call, Exception e, int id) {
                        }

                        @Override
                        public void onResponse(TransMessage response, int id) {
                            ArticleDiscussVo item = his.get(position);
                            if (item.getFollowNum() != null) {
                                item.setFollowNum(item.getFollowNum() + 1);
                            }
                            his.set(position, item);
                            notifyDataSetChanged();
                        }
                    });
                }
            });
        }
        if (item.getFollowNum() != null && item.getFollowNum() > 0) {
            holder.dianzan.setOnClickListener(null);
            holder.dianzan.setCompoundDrawablesWithIntrinsicBounds(null,
                    null, context.getResources().getDrawable(R.mipmap.zan_press), null);
            holder.dianzan.setTextColor(Color.parseColor("#2479E2"));
        }


        return v;
    }

    class ViewHolder {
        CircularImage head;
        TextView name, content, time, dianzan;
    }
}
